package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Student;
import com.example.demo.entity.User;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 专门用来处理ajax请求的controller
 */
@RestController
public class AjaxController {
    @Autowired
    UserMapper userMapper;

    @Autowired
    StudentMapper studentMapper;

    @RequestMapping("/userLogin")
    public String userLogin(@RequestParam("username") String username,
                            @RequestParam("password") String password,
                            HttpSession session){


        QueryWrapper qw  =  new QueryWrapper();
        qw.eq("username",username);
        qw.eq("password",password);
        User user = userMapper.selectOne(qw);
        if(user == null){
            return "您的账号密码存在错误";
        }else{
            session.setAttribute("user",user);
            return "1";
        }



    }


    @RequestMapping("addStudentByUser")
    public int addStudentByUser(@RequestBody Student stu){
        System.out.println(stu.toString());
        return  studentMapper.insert(stu);

    }


    @PostMapping("/registStudent")
    public int registStudent(@RequestBody User user){
        System.out.println("成功了");
        System.out.println(user.toString());
       return userMapper.insert(user);
    }


    @RequestMapping("/updateUserByField")
    public int updateUserByField(@RequestParam("id") int id,
                                 @RequestParam("field") String field,
                                 @RequestParam("value") String value){

         return    userMapper.updateUserByField(id, field, value);
    }


    @RequestMapping("/updateStudentById")
    public int  updateUserById(@RequestBody Student student){

        return  studentMapper.updateById(student);
    }
}
